﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using Vote.DataEntity;

namespace Vote.Database.DataAccess
{
    public class ActionAccess
    {
        /// <summary>
        /// 获取最近100条活动
        /// </summary>
        /// <returns></returns>
        public List<ActionDataEntity> GetActionList(Int32 index = 1, Int32 count = 5)
        {
            var actionList = new List<ActionDataEntity>();
            string sql = @"SELECT 
                            a.Id, a.Actionname,a.Begintime, a.Endtime, a.CreateTime,
                            (SELECT COUNT(b.Id) FROM vote b WHERE b.ActionId=a.Id ) as VoteCount
                        FROM 
                            `action` a
                        WHERE
                            a.Id <= (SELECT Id FROM `action` ORDER BY Id desc LIMIT {0}, 1) 
                        ORDER BY a.Id desc LIMIT {1}";
            sql = String.Format(sql, (index - 1) * count, count);
            DateTime result = DateTime.MinValue;
            using (var reader = MySqlHelper.ExecuteReader(MySqlHelper.ConnectionStringManager, CommandType.Text, sql, null))
            {
                while (reader.Read())
                {
                    var action = new ActionDataEntity()
                    {
                        ID = Convert.ToInt32(reader["ID"]),
                        ActionName = reader["Actionname"] != DBNull.Value ? reader["Actionname"].ToString() : string.Empty,
                        CreateTime = reader["CreateTime"] != DBNull.Value ? Convert.ToDateTime(reader["CreateTime"]) : DateTime.Now,
                        EndTime = reader["Endtime"] != DBNull.Value ? Convert.ToDateTime(reader["Endtime"]) : DateTime.Now,
                        BeginTime = reader["Begintime"] != DBNull.Value ? Convert.ToDateTime(reader["Begintime"]) : DateTime.Now,
                        VoteCount = Convert.ToInt32(reader["VoteCount"])
                    };
                    actionList.Add(action);
                }
            }

            return actionList;
        }

        /// <summary>
        /// 根据actionid获取单个投票分组
        /// </summary>
        /// <param name="actionId"></param>
        /// <returns></returns>
        public List<ItemDataEntity> GetVoteItemList(int actionId)
        {
            List<ItemDataEntity> result = new List<ItemDataEntity>();
            String sql = "SELECT P1.ActionID, P1.ActionName, P1.ActionDesc, P1.GroupId, P1.GroupName, P1.GroupDesc, P1.Itemid, P1.ItemName,P1.MinNum,P1.MaxNum,P1.Begintime, P1.Endtime,P1.VoteType,P1.ImageUrl, P1.Videourl, P1.Desc FROM ItemView AS P1 WHERE P1.ActionID=?actionid;";
            MySqlParameter parm = new MySqlParameter("actionid", MySqlDbType.Int32);
            parm.Value = actionId;
            using (MySqlDataReader reader = MySqlHelper.ExecuteReader(MySqlHelper.ConnectionStringManager, CommandType.Text, sql, parm))
            {
                while (reader.Read())
                {
                    ItemDataEntity item = new ItemDataEntity();

                    item.ItemID = Convert.ToInt32(reader["Itemid"]);
                    item.ItemName = reader["ItemName"] != DBNull.Value ? reader["ItemName"].ToString() : string.Empty;
                    item.VoteType = reader["VoteType"] != DBNull.Value ? Convert.ToInt32(reader["VoteType"]) : 0;
                    item.ImageUrl = reader["ImageUrl"] != DBNull.Value ? reader["ImageUrl"].ToString() : string.Empty;
                    item.RelationVideo = reader["Videourl"] != DBNull.Value ? reader["Videourl"].ToString() : string.Empty;
                    item.Desc = reader["Desc"] != DBNull.Value ? reader["Desc"].ToString() : string.Empty;
                    item.GroupID = Convert.ToInt32(reader["GroupId"]);
                    item.GroupName = reader["GroupName"] != DBNull.Value ? reader["GroupName"].ToString() : string.Empty;
                    item.GroupDesc = reader["GroupDesc"] != DBNull.Value ? reader["GroupDesc"].ToString() : string.Empty;
                    item.ActionID = Convert.ToInt32(reader["ActionID"]);
                    item.ActionName = reader["ActionName"] != DBNull.Value ? reader["ActionName"].ToString() : string.Empty;
                    item.ActionDesc = reader["ActionDesc"] != DBNull.Value ? reader["ActionDesc"].ToString() : string.Empty;
                    item.Begintime = reader["Begintime"] != DBNull.Value ? Convert.ToDateTime(reader["Begintime"]) : DateTime.MinValue;
                    item.Endtime = reader["Endtime"] != DBNull.Value ? Convert.ToDateTime(reader["Endtime"]) : DateTime.MinValue;
                    item.MinNum = reader["MinNum"] != DBNull.Value ? Convert.ToInt32(reader["MinNum"]) : 1;
                    item.MaxNum = reader["MaxNum"] != DBNull.Value ? Convert.ToInt32(reader["MaxNum"]) : 1;
                    result.Add(item);
                }
            }
            return result;
        }

        /// <summary>
        /// 根据actionid获取关联图片
        /// </summary>
        /// <param name="actionId"></param>
        /// <returns></returns>
        public List<string> GetImageUrlList(int actionId)
        {
            List<string> result = new List<string>();
            String sql = "SELECT P1.ImageUrl FROM ItemView AS P1 WHERE P1.ActionID=?actionid and P1.VoteType = 1;";
            MySqlParameter parm = new MySqlParameter("actionid", MySqlDbType.Int32);
            parm.Value = actionId;
            using (MySqlDataReader reader = MySqlHelper.ExecuteReader(MySqlHelper.ConnectionStringManager, CommandType.Text, sql, parm))
            {
                while (reader.Read())
                {
                    var imageUrl = reader["ImageUrl"] != DBNull.Value ? reader["ImageUrl"].ToString() : string.Empty;
                    if (!string.IsNullOrWhiteSpace(imageUrl))
                    {
                        result.Add(imageUrl);
                    }
                }
            }
            return result;
        }
    }
}
